package com.example.demo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

/**
 * @date 2017-07-05
 * @version V1.0
 */
@Controller
public class TestController {
	public static String[] YIWEI = { "143052", "514320", "205431", "051243", "320514", "432105" };
	public final static int[] a0 = { 8, 0, 2, 6, 4, 5, 7, 9, 1, 3 };
	public final static int[] a1 = { 3, 4, 8, 0, 2, 6, 1, 5, 7, 9 };
	public final static int[] a2 = { 2, 5, 9, 7, 3, 0, 6, 4, 8, 1 };
	public final static int[] a3 = { 9, 1, 0, 4, 7, 2, 5, 3, 6, 8 };
	public final static int[] a4 = { 6, 8, 1, 5, 0, 3, 4, 7, 9, 2 };
	public final static int[] a5 = { 7, 2, 3, 9, 5, 1, 8, 0, 4, 6 };
	public final static int[] a6 = { 4, 3, 7, 2, 6, 8, 9, 1, 5, 0 };
	public final static int[] a7 = { 1, 6, 4, 3, 9, 7, 2, 8, 0, 5 };
	public final static int[] a8 = { 0, 9, 5, 1, 8, 4, 3, 6, 2, 7 };
	public final static int[] a9 = { 5, 7, 6, 8, 1, 9, 0, 2, 3, 4 };

	public final static int[][] as = { a0, a1, a2, a3, a4, a5, a6, a7, a8, a9 };

	@RequestMapping(value = "/getPwd")
	@ResponseBody
	public String getPwd(@RequestBody String params) {
		System.out.println("test 收到的参数=====" + params);

		JSONObject retObj = new JSONObject();
		retObj.put("code", "0000");

		try {
			
			JSONObject po = JSONObject.parseObject(params);

			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

//			Date date = formatter.parse("2021-10-12 11:05");
			Date date = formatter.parse(po.getString("sysTime"));

			int nianYue = Integer.parseInt(reqDateNianYue(date));
			int shiFen = Integer.parseInt(reqDateShifen(date));
			System.out.println("年月 === " + nianYue + "时分  = " + shiFen);
			int sum = nianYue + shiFen;
			System.out.println("和 === " + sum);
			// 管理员密码
			String adminPwd = po.getString("adminPwd");
			
			int pwd = Integer.parseInt(adminPwd);
			String pwdYiwei = yiwei(adminPwd, pwd % 6);
			
			String yiweiStr = yiwei(sum + "", sum % 6);
			if (!StringUtils.isBlank(po.getString("randomCode"))) {
				yiweiStr = po.getString("randomCode");
			}
			// 移动位置
			System.out.println("移动顺序后" + yiweiStr);
			// 置换表
			String getPwd = zhihuan(yiweiStr, pwdYiwei);
			System.out.println("最后的密码" + getPwd);
			
			retObj.put("retCode", "0000");
			retObj.put("retMsg", "获取密码成功");
			retObj.put("lockPwd", "12"+getPwd);
			return retObj.toJSONString();

		} catch (Exception e) {
			retObj.put("retCode", "0001");
			retObj.put("retMsg", "获取密码未知异常！");
			return retObj.toJSONString();
		}
	}

	public static String reqDateNianYue(Date date) {
		return new SimpleDateFormat("yyyyMM").format(date);
	}

	public static String reqDateShifen(Date date) {
		return new SimpleDateFormat("ddHHmm").format(date);
	}

	public static String zhihuan(String yiweiStr, String pwd) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < yiweiStr.length(); i++) {
			int zong = Integer.parseInt(yiweiStr.charAt(i) + "");
			int heng = Integer.parseInt(pwd.charAt(i) + "");
			sb.append(as[heng][zong]);
		}
		return sb.toString();
	}

	public static String yiwei(String params, int bizType) {

		String key = YIWEI[bizType];
		StringBuilder sb = new StringBuilder("123456");
		for (int i = 0; i < key.length(); i++) {
			int shunxu = Integer.parseInt(key.charAt(i) + "");

			sb.replace(shunxu, shunxu + 1, params.charAt(i) + "");
		}

		return sb.toString();
	}

}
